import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SGuildFlameOpen } from "../../../Message/Public/SGuildFlameOpen";
import { FlameIconView } from "../view/guildFlame/FlameIconView";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { DataEvent } from "../events/DataEvent";
import { SystemSetter } from "../view/system/SystemSetter";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { GameProxy } from "../mvc/GameProxy";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { IFlameCountView } from "../view/guildFlame/IFlameCountView";
import { FlameCountView } from "../view/guildFlame/FlameCountView";
type int = number;
//class GuildFlameController
    
    export  class GuildFlameController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.GuildFlameOpen, this.onGuildFlameOpen);
            NetDispatcher.addCmdListener(ServerCommand.GuildFlameClose, this.onGuildFlameClose);
            NetDispatcher.addCmdListener(ServerCommand.GuildFlameBig, this.onGuildFlameBig);
            NetDispatcher.addCmdListener(ServerCommand.GuildFlameShort, this.onGuildFlameShort);
            NetDispatcher.addCmdListener(ServerCommand.GuildFlameExpAddCommand, this.onExpAddCommand);
            NetDispatcher.addCmdListener(ServerCommand.GuildFlameNimbusAddCommand, this.onNimbusAddCommand);
            NetDispatcher.addCmdListener(ServerCommand.ActiveAddGuildFlame, this.onActiveAddGuildFlameHandler);
            Dispatcher.addEventListener(EventName.GuildQuickBackFlame, this.onQuickBackFlame);
            Dispatcher.addEventListener(EventName.GuildFlameAddFireWood, this.onAddFireWood);
            Dispatcher.addEventListener(EventName.GuildAddFireRes, this.onGuildAddFireRes);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            return;
        }

        /* internal  */onGuildFlameOpen(arg1: SGuildFlameOpen): void
        {
            this._isInGuildFlameTime = true;
            FlameIconView.instance.show();
            FlameIconView.instance.startCountdown(arg1.endDt);
            this.startCoolDownTimer();
            this.resetGuildCountView();
            return;
        }

        /* internal  */onGuildFlameClose(arg1: Object): void
        {
            this._isInGuildFlameTime = false;
            FlameIconView.instance.hide();
            this.stopCoolDownTimer();
            this.resetGuildCountView();
            return;
        }

        /* internal  */onGuildFlameBig(arg1: Object): void
        {
            MsgManager.showTaskTarget(Language.getString(41700), 5);
            return;
        }

        /* internal  */onGuildFlameShort(arg1: Object): void
        {
            MsgManager.showTaskTarget(Language.getString(41701), 5);
            return;
        }

        /* internal  */onExpAddCommand(arg1: int): void
        {
            if (this._flameCountView && !this._flameCountView.isHide) 
            {
                this._countExp = this._countExp + arg1;
                this.guildCountView.updateExperience(this._countExp);
            }
            return;
        }

        /* internal  */onNimbusAddCommand(arg1: int): void
        {
            if (this._flameCountView && !this._flameCountView.isHide) 
            {
                this._countNimbus = this._countNimbus + arg1;
                this.guildCountView.updateNimbus(this._countNimbus);
            }
            return;
        }

        /* internal  */onActiveAddGuildFlameHandler(arg1: SActiveToClient): void
        {
            if (this._isInGuildFlameTime && !FlameIconView.instance.isHide) 
            {
                FlameIconView.instance.updateActiveToClientInfo(arg1);
            }
            return;
        }

        /* internal  */startCoolDownTimer(): void
        {
            if (cache.guildFlame.guildFrameOpen.addFirewoodCd > 0) 
            {
                if (!this._coolDonwTimer) 
                {
                    this._coolDonwTimer = new SecTimer();
                    this._coolDonwTimer.addListener(TimerType.ENTERFRAME, this.onTimerHandler);
                    this._coolDonwTimer.addListener(TimerType.COMPLETE, this.onTimerComHandler);
                }
                this._coolDonwTimer.repeatCount = cache.guildFlame.guildFrameOpen.addFirewoodCd;
                if (!this._coolDonwTimer.running) 
                {
                    this._coolDonwTimer.start();
                }
            }
            else 
            {
                this.stopCoolDownTimer();
            }
            return;
        }

        /* internal  */stopCoolDownTimer(): void
        {
            if (this._coolDonwTimer && this._coolDonwTimer.running) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildFlameWoodCoolTimeUpdate, 0));
                this._coolDonwTimer.stop();
                this._coolDonwTimer.isDelete = false;
            }
            return;
        }

        /* internal  */onTimerHandler(arg1: SecTimer): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildFlameWoodCoolTimeUpdate, arg1.repeatCount));
            return;
        }

        /* internal  */onTimerComHandler(arg1: SecTimer): void
        {
            var loc1=null;
            this.stopCoolDownTimer();
            if (SystemSetter.currentSetter.autoAddFire && NpcEffectRule.checkDistance(cache.scene.getGuildFlameNPCInfo())) 
            {
                loc1 = cache.pack.backPackCache.getItemsByJudge(ItemsUtil.isFireWood);
                if (loc1.length > 0) 
                {
                    this._addWoodTimerKey = flash.utils.setTimeout(this.onTimerComImpl, 1000);
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(41702));
                }
            }
            return;
        }

        /* internal  */onTimerComImpl(): void
        {
            GameProxy.guildProxy.addFireWood(1);
            return;
        }

        /* internal  */onGuildAddFireRes(arg1: DataEvent): void
        {
            if (cache.guildFlame.isAtFlameTime()) 
            {
                cache.guildFlame.guildFrameOpen.addFirewoodCd = 10;
                this.startCoolDownTimer();
            }
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (!this._isInGuildMap && GameMapUtil.isGuildMap()) 
            {
                this._isInGuildMap = true;
                this.resetGuildCountView();
            }
            else if (this._isInGuildMap && !GameMapUtil.isGuildMap()) 
            {
                this._isInGuildMap = false;
                this.resetGuildCountView();
            }
            return;
        }

        /* internal  */onQuickBackFlame(arg1: DataEvent): void
        {
            if (cache.guild.myGuildInfo) 
            {
                GameProxy.copy.enterGuild_async(cache.guild.myGuildInfo.guildId, false);
            }
            return;
        }

        /* internal  */onAddFireWood(arg1: DataEvent): void
        {
            flash.utils.clearTimeout(this._addWoodTimerKey);
            var loc1=cache.pack.backPackCache.getItemsByJudge(ItemsUtil.isFireWood);
            if (loc1.length > 0) 
            {
                GameProxy.guildProxy.addFireWood(1);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(41702));
            }
            return;
        }

        /* internal  */resetGuildCountView(): void
        {
            if (this._isInGuildMap && this._isInGuildFlameTime) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
                this.guildCountView.show();
                this.guildCountView.resetPosition();
            }
            else 
            {
                if (this._flameCountView) 
                {
                    if (!this.guildCountView.isHide) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
                    }
                    this.guildCountView.hide();
                    this.guildCountView.clear();
                }
                this._countExp = 0;
                this._countNimbus = 0;
            }
            return;
        }

        public get guildCountView(): IFlameCountView
        {
            if (!this._flameCountView) 
            {
                this._flameCountView = new FlameCountView();
            }
            return this._flameCountView;
        }

        public isInitCountView(): Boolean
        {
            return !(this._flameCountView == null);
        }

        private /* var */_coolDonwTimer: SecTimer;

        private /* var */_isInGuildMap: Boolean=false;

        private /* var */_isInGuildFlameTime: Boolean=false;

        private /* var */_countExp: int=0;

        private /* var */_countNimbus: int=0;

        private /* var */_addWoodTimerKey: uint;

        private /* var */_flameCountView: IFlameCountView;
    }
